package com.meizu.asum.richtextsdk.tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * Description：计算比例工具 <br>
 * User：Xie JunWen <br>
 * Email：v-xiejunwen@meizu.com <br>
 * CreateTime：2018/1/17  11:36 <br>
 * Remark：无
 */
public class ComputeScaleTools {
    public static int computeScale(BitmapFactory.Options options, int maxWidth, int maxHeight) {
        int inSampleSize = 1;
        if (maxWidth == 0 || maxHeight == 0) {
            return inSampleSize;
        }
        int bitmapWidth = options.outWidth;
        int bitmapHeight = options.outHeight;
        if (bitmapWidth > maxWidth || bitmapHeight > maxHeight) {
            int widthScale = Math.round((float) bitmapWidth / (float) maxWidth);
            int heightScale = Math.round((float) bitmapHeight / (float) maxHeight);

            inSampleSize = widthScale > heightScale ? widthScale : heightScale;
        }
        return inSampleSize;
    }

    public static int computeScale(Bitmap bitmap, int maxWidth, int maxHeight) {
        if (bitmap == null) {
            return 1;
        }
        int inSampleSize = 1;
        if (maxWidth == 0 || maxHeight == 0) {
            return inSampleSize;
        }
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        if (bitmapWidth > maxWidth || bitmapHeight > maxHeight) {
            int widthScale = Math.round((float) bitmapWidth / (float) maxWidth);
            int heightScale = Math.round((float) bitmapHeight / (float) maxHeight);

            inSampleSize = widthScale > heightScale ? widthScale : heightScale;
        }
        return inSampleSize;
    }
}
